<template>
	<view :class="themeStyle">
		<mescroll-uni @getData="getData" refs="mescroll">
			<block slot="list">
				<!-- 列表数据 -->
				<view class="station-list" v-if="dataList.length">
				
					<view class="station-item" v-for="(item, index) in dataList" :key="index">
						<view class="station-header">
							<view class="station-icon">
								<image class="image" :src="$util.img(item.station_icon)" mode="aspectFill" />
							</view>
							<view class="station-header-right">
								<view class="station-title">{{ item.title }}</view>
								<view class="ns-font-size-sm ns-text-color-gray station-time">{{$util.timeStampTurnTime(item.create_time)}}</view>
							</view>
						</view>
						<view class="station-bottom">
							<view class="station-content" ><rich-text :nodes="item.content"></rich-text></view>
							
							<view class="station-action">
								<view class="ns-font-size-sm ns-text-color-gray station-btn" @click="deleteStation(item.station_id, index)">删除</view>
							</view>
						</view>
					</view>
				</view>
				
				<!-- 为空样式 -->
				<view v-else class="page-empty"><ns-empty :isIndex="!1"></ns-empty></view>
				
				<!-- 分页加载中提示 -->
				<loading-cover ref="loadingCover"></loading-cover>
			</block>
		</mescroll-uni>
	</view>
</template>

<script>
export default {
	data() {
		return {
			dataList: [],
			isSubmit: false
		};
	},
	onLoad() {},
	
	onShow() {	
		
		// 验证登录
		if (uni.getStorageSync('token')) {
			if (this.$refs.mescroll) this.$refs.mescroll.refresh();
		} else {
			this.$util.redirectTo('/pages/login/login/login', {
				back: '/pages/station/list/list'
			});
		}
	},
	computed: {
		//vueX页面主题
		themeStyle() {
			return 'theme-' + this.$store.state.index.themeStyle;
		}
	},
	methods: {
		/**
		 * 分页数据
		 * @param {Object} mescroll
		 */
		getData(mescroll) {
			this.$api.sendRequest({
				url: '/api/Message/stationList',
				data: {
					page_size: mescroll.size,
					page: mescroll.num
				},
				success: res => {
					let dataList = [];
					
					if (res.code >= 0) {
						dataList = res.data.list;
					} else {
						this.$util.showToast({title: res.message});
					}
					
					// 设置列表数据
					mescroll.endSuccess(dataList.length);
					if (mescroll.num == 1) this.dataList = [];
					this.dataList = this.dataList.concat(dataList);
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res => {
					mescroll.endErr();
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		
		/**
		 * 删除消息
		 * @param {Object} station_id
		 */
		deleteStation(station_id, index) {
			
			uni.showModal({
				title: '提示',
				content: '确定要删除吗？',
				success: res => {
					if (res.confirm) {
						
						if (this.isSubmit) return;
						this.isSubmit = false;
						
						this.$api.sendRequest({
							url: '/api/Message/deleteStationRecords',
							data: {
								station_ids: station_id
							},
							success: res => {
								this.isSubmit = false;
								
								if (res.code >= 0) {
									this.dataList.splice(index, 1)
								} else {
									this.$util.showToast({title: res.message});
								}
							},
							fail: res => {
								this.isSubmit = false;
							}
						});
					}
				}
			})
			
			
		}
	}
};
</script>

<style lang="scss">
/deep/ 

.fixed {
	position: relative;
	top: 0;
}
.page-empty {
	position: relative;
	top: -50%;
	padding-top: 154px;
}


.station-list{
	width: 100%;
	padding: 30rpx 3%;
	box-sizing: border-box;
	
	.station-item{
		margin-bottom: 30rpx;
		padding: 20rpx;
		background-color: #FFF;
		border-radius: 12rpx;
		box-shadow: 0 2px 4px rgba(0, 0, 0, .12);
		
		.station-header{
			display: flex;
			border-bottom: 1px solid #f3f3f3;
			padding-bottom: 20rpx;
			
			.station-icon{
				width: 100rpx;
				height: 100rpx;
				min-width: 100rpx;
				min-height: 100rpx;
				max-width: 100rpx;
				max-height: 100rpx;
				line-height: 100rpx;
				text-align: center;
				margin-left: 0;
				
				.image{
					width: 100%;
					height: 100%;
					max-height: 100%;
					max-width: 100%;
				}
			}
			
			.station-header-right{
				margin-left: 28rpx;
				max-width: 100%;
				overflow: hidden;
				
				.station-title{
					font-weight: bold;
					max-width: 100%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
		}
		
		.station-bottom{
			padding: 0 10rpx;
			
			.station-content{
				padding: 20rpx 0;
			}
			
			.station-action{
				display: flex;
				justify-content: flex-end;
			}
			
		}
	}
}


</style>
